package com.wondersgroup.fjzlpt.yyzyzx.dao.impl.yyzygl.yysz;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.wondersgroup.fjzlpt.yyzyzx.dao.inter.yyzygl.yysz.JgkhgzDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;

@Repository
public class JgkhgzDaoImpl extends BaseJTDAOImpl implements JgkhgzDao {

	@Override
	@Autowired
	@Qualifier("TB_YYZY_JGKHGZ")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}

	/**
	 * 根据"规则编号"查询所有的关联机构
	 * @param map
	 * @return
	 */
	@Override
	public JSONArray queryAll(Map map) {
		JSONObject sqlObj = this.getQuerySql(map);
		
		List sqlParams = sqlObj.getJSONArray("sqlParams");
		SQLBean bean = new SQLBean(sqlObj.getString("sql"),sqlParams.toArray());
		
		return super.queryList(bean);
	}
	
	/**
	 * 合成查询sql
	 * @param map
	 * @return
	 */
	public JSONObject getQuerySql(Map<String,String> map){
		JSONObject result = new JSONObject();
		
		StringBuffer sqlBuf = new StringBuffer();
		sqlBuf.append("select * from ").append(this.tb.getTableName()).append(" where 1=1 ");
		
		List<String> sqlParams = new ArrayList<String>();
		//规则编号
		String value = map.get("GZBH");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append("and GZBH = ? ");
			sqlParams.add(value);
		}
		
		result.put("sql", sqlBuf.toString());
		result.put("sqlParams", sqlParams);
		
		return result;
	}

	/**
	 * 根据"规则编号"删除所有的关联机构
	 * @param map
	 * @return
	 */
	@Override
	public void deleteByGzbh(String GZBH) {
		
		StringBuffer sqlBuf = new StringBuffer();
		sqlBuf.append("delete from ").append(this.tb.getTableName()).append(" where GZBH = ?");
		
		SQLBean bean = new SQLBean(sqlBuf.toString(), GZBH);
		super.update(bean);
	}
}
